#!/usr/bin/env tclsh
# $Id: test10.tcl 9ce65bed8ef4 2012/08/26 02:14:51 pgurumur $
# Copyright (c) 2012 Prabhu Gurumurthy <pgurumur@gmail.com>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#

# __version__  = "$Revision: 9ce65bed8ef4 $"
# __author__   = "$Author: pgurumur $"
# __modified__ = "$Date: 2012-08-26 02:14:51Z $"

lappend ::auto_path venya
package require stdlib

namespace eval dict {
   namespace export open
   namespace export close
   namespace export cmdline

   set _channel ""
   set _os ""
   set _mac ""
}

proc dict::err mesg {
   if {[string length $mesg]} {
      return -code error -errorinfo $mesg -errorcode 1
   } else {
      return -code error -errorinfo "Unknown error" -errorcode 1
   }
}

proc dict::open { host { port 2628 } } {
   global _channel
   set retval ""

   if {[string length $host] && $port} {
      if {[catch {set retval [socket $host $port]} err]} {
         dict::err "$err $::errorInfo"
      } else {
         fconfigure $retval -translation auto 
         fconfigure $retval -blocking 0 -buffering none
         fileevent $retval readable [list dict::sockread $retval]
         set _channel $retval
      }
   }
}

proc dict::shutdown {} {
   global _channel

   if {[string length $_channel]} {
      close $_channel
   }
}

proc dict::sockwrite command {
   global _channel

   if {[string length $command]} {
      puts -nonewline $_channel $command
      dict::sockread $_channel
   }
}

proc dict::sockread channel {
   set flag 1
   while {$flag} {
      set data [read $channel 512]
      if {[string length $data]} {
         fconfigure stdout -buffering none
         puts -nonewline stdout $data
      } 

      if {[fblocked $channel] == 0} {
         set flag 0
      }
   }
}

proc dict::cmdline {} {
   global _channel _os _mac tcl_platform

   if {[string length $_channel]} {
      set _os $tcl_platform(os)
      set _mac $tcl_platform(machine)
      set flag 1
      set info [format "client info %s %s version 0.1\r\n" $_os $_mac]
      dict::sockwrite $info
      puts {}
      while {$flag} {
         puts -nonewline "dict > "
         set command [gets stdin]
         if {[string length $command]} {
            set command [concat "$command\r\n"]
            if {$command == "exit"} {
               set flag 0
            } 

            dict::sockwrite $command
         }
      }
   }
}

dict::open "dict.org" 2628
dict::cmdline
dict::shutdown

